﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToRTR.CApExportToRTRRobotSupplement
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExportToRTR.YAML;
using System.Collections.Generic;
using System.IO;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToRTR{

public class CApExportToRTRRobotSupplement : CApExportToRTRExporter
{
  private ITxLocatableObject m_supplement;
  protected ITxLocatableObject m_attachmentParent;
  protected TxRobot m_robot;

  internal List<ITxObject> ObjectsWith3D { get; set; } = new List<ITxObject>();

  internal List<ITxObject> ChildrenWith3D { get; set; } = new List<ITxObject>();

  public CApExportToRTRRobotSupplement(
    ITxLocatableObject supplement,
    ITxLocatableObject attachmentParent,
    TxRobot robot,
    string root)
    : base(root, "", "")
  {
    this.m_supplement = supplement;
    this.m_attachmentParent = attachmentParent;
    this.m_robot = robot;
    this.m_directory = "cad";
  }

  public virtual void Export()
  {
    if (!Directory.Exists(Path.Combine(this.m_root, this.m_directory)))
      Directory.CreateDirectory(Path.Combine(this.m_root, this.m_directory));
    if (!(this.Export(this.m_supplement) != "emptyLink"))
      return;
    this.ObjectsWith3D.Add((ITxObject) this.m_supplement);
  }

  protected override void CreateMate(ITxLocatableObject obj, ObjectInstance yamlObj)
  {
    if (this.m_attachmentParent is TxPhysicalRoot)
      CApExportToRTRYamlMgr.Instance.CreateMateToReferenceFrame(obj, (IObjectInstance) yamlObj);
    else
      this.CreateMateToAttachmentParent(yamlObj);
  }

  private void CreateMateToAttachmentParent(ObjectInstance yamlObj)
  {
    TxTransformation origin = CApExportToRTRRobotSupplement.GetOrigin(this.m_supplement, this.m_attachmentParent, (ITxObjectCollection) this.m_robot);
    CApExportToRTRYamlMgr.Instance.AddObj<Mate>(new Mate()
    {
      Name = CApExportToRTRYamlUtils.GetMateUniqueName((ITxObject) this.m_attachmentParent, (ITxObject) this.m_supplement),
      Parent = CApExportToRTRRobotSupplement.CreateParentObj(this.m_attachmentParent, (ITxObjectCollection) this.m_robot),
      Child = yamlObj.CreateObjectRef(),
      OffsetXyz = new Vec3D(origin.Translation),
      OffsetRpy = new Vec3D(origin.GetRotation())
    });
  }

  public static TxTransformation GetOrigin(
    ITxLocatableObject obj,
    ITxLocatableObject attachmentParent,
    ITxObjectCollection robot)
  {
    RobotInstance robotInstance = CApExportToRTRYamlMgr.Instance.GetObj((ITxObject) robot) as RobotInstance;
    TxTransformation origin = obj.GetLocationRelativeToObject(attachmentParent);
    if (attachmentParent is TxKinematicLink && TxJoint.op_Inequality((attachmentParent as TxKinematicLink).ParentJoint, (ITxObject) null) && (((ITxObject) attachmentParent).Collection == robot || CApExportToRTRRobotSupplement.IsJointExported(attachmentParent as TxKinematicLink)))
    {
      TxTransformation relativeToObject = attachmentParent.GetLocationRelativeToObject(((ITxObject) attachmentParent).Collection as ITxLocatableObject);
      origin = TxTransformation.op_Multiply((((ITxObject) attachmentParent).Collection == robot ? CApExportToRTRYamlMgr.Instance.JointsOffset[CApExportToRTRYamlUtils.GetRobotKinematicChildKey(robotInstance.RobotPrototype.Name, (ITxObject) (attachmentParent as TxKinematicLink).ParentJoint)] : new TxTransformation()).Inverse, TxTransformation.op_Multiply(relativeToObject, origin));
    }
    return origin;
  }

  private static bool IsJointExported(TxKinematicLink link)
  {
    IObj obj = CApExportToRTRYamlMgr.Instance.GetObj((ITxObject) link.ParentJoint);
    return obj != null && obj is Joint;
  }

  public static ObjectRef CreateParentObj(
    ITxLocatableObject attachmentParent,
    ITxObjectCollection robot)
  {
    RobotInstance robotInstance = CApExportToRTRYamlMgr.Instance.GetObj((ITxObject) robot) as RobotInstance;
    ObjectRef parentObj;
    if (attachmentParent is TxKinematicLink && ((ITxObject) attachmentParent).Collection == robot)
    {
      IObj robotKinematicChild = CApExportToRTRYamlMgr.Instance.GetRobotKinematicChild(robotInstance.RobotPrototype, (ITxObject) attachmentParent);
      parentObj = new ObjectRef()
      {
        Robot = robotInstance.Name,
        Link = robotKinematicChild.Name
      };
    }
    else if (attachmentParent == robot)
    {
      parentObj = robotInstance.CreateObjectRef();
    }
    else
    {
      IObj obj = CApExportToRTRYamlMgr.Instance.GetObj((ITxObject) attachmentParent);
      parentObj = new ObjectRef();
      if (attachmentParent is TxFrame)
        parentObj.Frame = obj.Name;
      else
        parentObj.Object = obj.Name;
    }
    return parentObj;
  }

  internal static IObj GetYamlParent(ITxLocatableObject attachmentParent, ITxObjectCollection robot)
  {
    RobotInstance robotInstance = CApExportToRTRYamlMgr.Instance.GetObj((ITxObject) robot) as RobotInstance;
    return !(attachmentParent is TxKinematicLink) || ((ITxObject) attachmentParent).Collection != robot ? CApExportToRTRYamlMgr.Instance.GetObj((ITxObject) attachmentParent) : CApExportToRTRYamlMgr.Instance.GetRobotKinematicChild(robotInstance.RobotPrototype, (ITxObject) attachmentParent);
  }
}
